package com.jeremy.aspect;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class ServiceLogAspect{
	
	private static final Logger logger = Logger.getLogger(ServiceLogAspect.class);
	
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable
	{
		logger.info("开始执行-------------" + joinPoint.getTarget().getClass()+ "." + (joinPoint.getSignature().getName())+"方法");
		
		Object object = joinPoint.proceed();
		
		logger.info("执行完毕-------------" + joinPoint.getTarget().getClass()+ "." + (joinPoint.getSignature().getName())+"方法");
		
		return object;
	}
	
	public void excption(JoinPoint joinPoint, Throwable ex) {
		logger.error("出现异常-------------方法：" + joinPoint.getTarget().getClass()
				+ "." + joinPoint.getSignature().getName() + "-----异常信息："
				+ ex.getMessage() +"\r\n"+ getExceptionTrace(ex));
	}
	
	public static String getExceptionTrace(Throwable ex) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		ex.printStackTrace(pw);
		return sw.toString();
 
	}
}
